## Evaluation of Wahlomat experiment -----

# load packages
source("packages.r")


## data import ------------------------------

load("../data/yougov_panel_ger_wide_wahlomat_prep.RData")


# variables
assignment <- "wahlomat_encouragement"
treatment <- "wahlomat_use_combined"
covar_names <- c("female", "age.1", "west", "educ_presecondary", "educ_secondary", "educ_highschool", "hhinc_1500less", "hhinc_1500_3000", "hhinc_3000more", "polinterest.1", "civic_knowledge.2", "leftright.1", "voted_2013_yesno", "likelihoodvote.1", "votecertainty.1")


covar_names_labels <- c("Female [0,1]", 
                        paste0("Age [", range(waves_df_wide$age.1)[1], "-", range(waves_df_wide$age.1)[2], "]"),
                        "West Germany [0,1]",
                        "Education:\nMain school [0,1]",
                        "Education:\nSecondary school [0,1]",
                        "Education:\nHigh school [0,1]",
                        "Household income:\n<1500 EUR",
                        "Household income:\n1500-3000 EUR",
                        "Household income:\n>3000 EUR",
                        "Political interest [1,5]",
                        "Civic knowledge [0,2]",
                        "Political ideology [1,11]",
                        "Voted 2013 [0,1]",
                        "Likelihood to vote [1,10]",
                        "Vote certainty [1,10]")
                        

## run models ------------------------------

# loop
out_list <- list()
for (i in seq_along(covar_names)) {
  out_list[[i]] <- with(waves_df_wide, ivdesc(X = eval(parse(text = covar_names[i])), 
                                    D = wahlomat_use_combined, 
                                    Z = wahlomat_encouragement)) %>%
              as.data.frame()
  out_list[[i]]$covar <- covar_names[i]
  out_list[[i]]$covar_label <- covar_names_labels[i]
  print(paste0(i, "..."))
}
out_df <- bind_rows(out_list)

# re-define group variable
out_df$group_label <- recode_factor(out_df$group, sample = "Sample", co = "Compliers", nt = "Never-takers", at = "Always-takers")
out_df$covar_label <- factor(out_df$covar_label, levels = covar_names_labels)

## plot outcomes ---------------------------

p <- ggplot(out_df, aes(group_label, mu)) + 
  geom_pointrange(aes(ymin = mu - 1.96*mu_se, ymax = mu + 1.96*mu_se), shape = 19, fatten = 1, size = 1, stat="identity") + 
  geom_pointrange(aes(ymin = mu - 1.38*mu_se, ymax = mu + 1.38*mu_se), shape = 19, fatten = 1, size = 2, stat="identity") + 
  facet_rep_wrap(~ covar_label,  scales='free_x', repeat.tick.labels = 'none', ncol = 3) +  # from lemon package to get rid of panel-specific y axis labels
  coord_flip() + 
  theme_ipsum_rc() + 
  labs(y = "\nMean and 83%/95% confidence intervals") + 
  theme(panel.background = element_blank(), 
        axis.title.x = element_text(hjust = 0.5, size = 12), 
        axis.title.y = element_blank(), 
        axis.text = element_text(size = 12), 
        plot.caption = element_text(size = 7), 
        strip.text.x = element_text(size = 12, colour = "darkblue", face = "bold"),
        plot.margin=unit(c(0.1,0.1,0.1,0.1), "cm"),
        panel.spacing = unit(0.25, "cm"))
p
# save plot
ggsave(p, file = "../output/wahlomat-complier-profiling.png",
       width = 4, height = 5, units = "cm", dpi = 300, scale = 5)




